package Tests
{
	import Box2D.Collision.Shapes.b2EdgeShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.Joints.b2PrismaticJoint;
	import Box2D.Dynamics.Joints.b2PrismaticJointDef;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2BodyType;
	
	import Framework.Test;
	
	public class Prismatic extends Test
	{
		private var m_joint:b2PrismaticJoint
		public function Prismatic()
		{
			super();
			createGround();
			createJoint();
		}
		private function createGround():void{
			var shape:b2EdgeShape = new b2EdgeShape;
			shape.Set(new b2Vec2(-40,0),new b2Vec2(40,0));
			m_groundBody.CreateFixtureByShape(shape,0);
		}
		private function createJoint():void{
			var shape:b2PolygonShape = new b2PolygonShape;
			shape.SetAsBox(2.0, 0.5);
			
			var bd:b2BodyDef = new b2BodyDef;
			bd.type = b2BodyType.b2_dynamicBody;
			bd.position.Set(-10.0, 10.0);
			bd.angle = 0.5 * Math.PI;
			bd.allowSleep = false;
			var body:b2Body = m_world.CreateBody(bd);
			body.CreateFixtureByShape(shape, 5.0);
			
			var pjd:b2PrismaticJointDef = new b2PrismaticJointDef;
			
			// Bouncy limit
			var axis:b2Vec2 = new b2Vec2(2.0, 1.0);
			axis.Normalize();
			pjd.Initialize(m_groundBody, body, new b2Vec2(0.0, 0.0), axis);
			
			// Non-bouncy limit
			//pjd.Initialize(ground, body, b2Vec2(-10.0f, 10.0f), b2Vec2(1.0f, 0.0f));
			
			pjd.motorSpeed = 10.0;
			pjd.maxMotorForce = 10000.0;
			pjd.enableMotor = true;
			pjd.lowerTranslation = 0.0;
			pjd.upperTranslation = 20.0;
			pjd.enableLimit = true;
			
			m_joint = m_world.CreateJoint(pjd) as b2PrismaticJoint;

		}
		public override function Key(key:String):void{
			switch (key)
			{
				case 'l':
					m_joint.EnableLimit(!m_joint.IsLimitEnabled());
					break;
				
				case 'm':
					m_joint.EnableMotor(!m_joint.IsMotorEnabled());
					break;
				
				case 's':
					m_joint.SetMotorSpeed(-m_joint.GetMotorSpeed());
					break;
			}
		}
	}
}